﻿using Foundation;
using RestSharp;
using UIKit;
using Yidun.NTESQuickPassBinding;

namespace AudioDemo.iOS
{
    public class LoginController:UIViewController
	{
        NTESQuickLoginManager _quickloginManager;


        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            _quickloginManager = NTESQuickLoginManager.SharedInstance;
            _quickloginManager.RegisterWithBusinessID("01e659b45a3040f297c0824267ea39d7");
            //取号超时设置
            _quickloginManager.TimeoutInterval = 3000;
            //网络运营商是否支持一键登录
            if (_quickloginManager.ShouldQuickLogin)
            {
                //预取号
                _quickloginManager.GetPhoneNumberCompletion((result) => {
                    NSNumber booNumber = (NSNumber)result.ObjectForKey(new NSString("success"));
                    var token = result.ObjectForKey(new NSString("token"));
                    var success = booNumber == NSNumber.FromBoolean(true);
                    if (success)
                    {
                        SetCustomUI();
                        AuthorizeLogin(token.ToString());
                    }
                });
            }

        }
        /// <summary>
        /// 设置授权UI界面
        /// </summary>
        private void SetCustomUI()
        {
            //https://support.dun.163.com/documents/287305921855672320?docId=429869784953151488
            var model = new NTESQuickLoginModel();
            model.PresentDirectionType = NTESPresentDirection.ush;
            //....

            _quickloginManager.SetupModel(model);
        }

        /// <summary>
        /// 拉取授权页面，该方法必须在设置授权页 UI 界面之后调用
        /// </summary>

        private void AuthorizeLogin(string token)
        {
            _quickloginManager.CUCMCTAuthorizeLoginCompletion((result) => {
                NSNumber booNumber = (NSNumber)result.ObjectForKey(new NSString("success"));
                var success = booNumber == NSNumber.FromBoolean(true);
                if (success)
                {
                    var accessToken = result.ObjectForKey(new NSString("accessToken"));
                    // 预取号和取号分别获取的token、accessToken，到服务端校验。
                    var phone = Validate(token,accessToken.ToString());
                    if (!string.IsNullOrEmpty(phone))
                    {
                        //取得手机号，登录注册
                    }
                }
                else
                {
                    //取号失败
                }
            });
        }

        private string? Validate(string token,string accessToken)
        {
            //https://support.dun.163.com/documents/287305921855672320?docId=288803729972580352
            var client = new RestClient("https://ye.dun.163.com/v1");
            var request = new RestRequest("oneclick/check").AddObject(new { token, accessToken });
            var result = client.Post<Result>(request);

            if (result?.Msg == "ok")
            {
                return result.Data?.Phone;
            }

            return null;
        }


    }

    public class Result
    {
        public string Code { get; set; }

        public string Msg { get; set; }

        public Data Data { get; set; }
    }
    public class Data
    {
        public string Phone { get; set; }

        public string ResultCode { get; set; }
    }
}

